package com.example.a1003notificationdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    NotificationManager manager;
    Button btnSend, btnDelete;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSend = (Button)findViewById(R.id.btnSend);
        btnDelete = (Button)findViewById(R.id.btnDelete);
        btnSend.setOnClickListener(new mClick());
        btnDelete.setOnClickListener(new mClick());
        manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    }

    class mClick implements View.OnClickListener{
        @Override
        public void onClick(View view){
            if(view==btnSend){
                // 下面两行，是在点击通知后，会将NotificationActivity.java的内容显示出来
                // NotificationActivity.java要在AndroidManifest.xml中注册，
                // 否则，点击通知是没有任何反应的
                Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(MainActivity.this,
                        1, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
                Notification notification = new NotificationCompat.Builder(
                        (Context) MainActivity.this, (Notification) null)
                        .setContentTitle("通知")
                        .setContentText("紧急假通知，马上下课！")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.drawable.ic_con)  //图标，大小14x14
//                        .setAutoCancel(true)   //与下面的cancel（）一样，打开通知后，不再显示在导航区
                        .setContentIntent(pi)   //点击通知，会弹出具体内容页面
                        .setPriority(NotificationCompat.PRIORITY_MAX)  //设置为最高权限，也可以设置其他
                        .build();
                manager.notify(1, notification);
            }else if(view==btnDelete){
                //manager.cancel(1);   //指定将manager.notify(1, notification);这个“1”的通知去掉。
                manager.cancelAll();
            }
        }
    }
}
